package com.iamsleeping;

import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Mohit on 05-Apr-14.
 */
public class AlarmPickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {
    Context context;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        context = getActivity();
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,  DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        
    	/*
    	// Do something with the time chosen by the user
        Time time = new Time();
        time.set(0, hourOfDay, minute, Calendar.DAY_OF_MONTH, Calendar.MONTH, Calendar.YEAR);
        Calendar cal= Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, view.getCurrentHour());
        cal.set(Calendar.SECOND, 0);


        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        final String ALARM_ACTION = "com.iamsleeping.AlarmReceiver";
        IntentFilter intentFilter = new IntentFilter(ALARM_ACTION);
        AlarmReceiver mReceiver = new AlarmReceiver(context);
        context.registerReceiver(mReceiver, intentFilter);
        Intent intent = new Intent(ALARM_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
        Toast.makeText(context, "Alarm set for"+pendingIntent, Toast.LENGTH_LONG).show();
        
        */
    	
    	 Calendar calNow = Calendar.getInstance();
    	   Calendar calSet = (Calendar) calNow.clone();

    	   calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
    	   calSet.set(Calendar.MINUTE, minute);
    	   calSet.set(Calendar.SECOND, 0);
    	   calSet.set(Calendar.MILLISECOND, 0);
    	   
    	   if(calSet.compareTo(calNow) <= 0){
    	    //Today Set time passed, count to tomorrow
    	    calSet.add(Calendar.DATE, 1);
    	   }
    	   
    	   setAlarm(calSet);
    	  };

    	 private void setAlarm(Calendar targetCal){
    		 
    		   AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    	        final String ALARM_ACTION = "com.iamsleeping.AlarmReceiver";
    	        IntentFilter intentFilter = new IntentFilter(ALARM_ACTION);
    	        AlarmReceiver mReceiver = new AlarmReceiver(context);
    	        context.registerReceiver(mReceiver, intentFilter);
    	        Intent intent = new Intent(ALARM_ACTION);
    	        intent.putExtra("NotifID", 1);
    	        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    	        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
    	        Toast.makeText(context, "Alarm set for"+targetCal.getTime(), Toast.LENGTH_LONG).show();

    	  /*
    	  Intent intent = new Intent(context, AlarmReceiver.class);
    	  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0);
    	  AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    	  alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
    	  */
    	  
    	 }
    	 
    	 
        
    

}
